<template>
  <!--进出场单-->
  <j-modal :title="title" :width="width" :visible="visible" switchFullscreen @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    :cancelButtonProps="{ class:{'jee-hidden': disableSubmit} }" @cancel="handleCancel" cancelText="关闭" okText="确定">
    <!--表单-->
    <a-spin :spinning="confirmLoading">
      <j-form-container class="scroll-view" style="height: 600px">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">

          <a-row style="margin-bottom: 20px;">
            <a-col :span="24">
              <span
                style="font-size: 20px; font-weight: bold;">{{title === '新增进场单'?'进场信息录入':title === '新增退场单'?'退场信息录入':''}}</span>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="24">
              <a-form-model-item label="选择设备" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="equipmentId"
                style="margin-bottom: 10px;">
                <div style="display: flex;flex-direction: column;">
                  <div style="display: flex;margin-bottom: 10px;">
                    <a-button @click="handleSelect('add','选择设备')" type="primary"
                      style="width: 100px;height: 40px;margin-right: 10px;">选择设备</a-button>
                    <a-button type="danger" @click="handleDelCom"
                      v-if="(selectedRowKeys && selectedRowKeys.length>0) && (pageType === 'add' || pageType === 'edit')"
                      style="width: 100px;height: 40px;text-align: center;">批量删除</a-button>
                  </div>

                  <a-table v-if="deviceList && deviceList.length>0" :columns="columns" :dataSource="deviceList" :pagination="false"
                    :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
                    style="margin-bottom: 20px;">
                    <template v-for="(col, i) in ['number', 'projectName', 'typeName','name','model']" :slot="col"
                      slot-scope="text, record, index">
                    </template>
                    <template slot="deletes" slot-scope="text, record, index">
                      <a-popconfirm title="是否要删除此行？" @confirm="remove(record.key)">
                        <a-icon type="close" />
                      </a-popconfirm>
                    </template>
                  </a-table>

                </div>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item label="填单时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="filingTime"
                style="margin-bottom: 10px;">
                <j-date :disabled="true" v-model="model.filingTime" :showTime="true" date-format="YYYY-MM-DD HH:mm:ss"
                  style="" placeholder="请选择填单时间" style="width: 100%;"></j-date>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="填写人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="filingProjectName"
                style="margin-bottom: 10px;">
                <div style="display: flex;align-items: center;">
                  <!-- <a-select :disabled="disabled" placeholder="请选择项目" :value="model.filingProjectName"
                    @change="handleChange('filingProject',$event)" style="margin-right: 10px;width: 112px;">
                    <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select> -->
                  <a-select :disabled="disabled" placeholder="请选择部门" :value="model.filingDepartName"
                    @change="handleChange('filingDepart',$event)" style="margin-right: 10px;width: 173px;">
                    <a-select-option v-for="(item,index) in filingDepartList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                  <a-select :disabled="true" placeholder="请选择人员" :value="model.filingUserName"
                    @change="handleChange('filingUser',$event)" style="width: 173px;">
                    <a-select-option v-for="(item,index) in filingUserList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="12">
              <a-form-model-item :label="title === '新增进场单'?'进场单号':title === '新增退场单'?'退场单号':''" :labelCol="labelCol"
                :wrapperCol="wrapperCol" prop="recordNumber" style="margin-bottom: 10px;">
                <a-input :disabled="disabled" v-model="model.recordNumber"
                  :placeholder="title === '新增进场单'?'请输入进场单号':title === '新增退场单'?'请输入退场单号':''"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item :label="title === '新增进场单'?'进场时间':title === '新增退场单'?'退场时间':''" :labelCol="labelCol"
                :wrapperCol="wrapperCol" prop="recordTime" style="margin-bottom: 10px;">
                <j-date :disabled="disabled" v-model="model.recordTime" :showTime="true" date-format="YYYY-MM-DD"
                  :placeholder="title === '新增进场单'?'请选择进场时间':title === '新增退场单'?'请选择退场时间':''"
                  style="width: 100%;"></j-date>
              </a-form-model-item>
            </a-col>
          </a-row>

          <template v-if="title === '新增进场单'">
            <a-row>
              <a-col :span="12">
                <a-form-model-item label="检测设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="statusName"
                  style="margin-bottom: 10px;">
                  <a-select :disabled="disabled" placeholder="请选择检测设备状态" :value="model.statusName"
                    @change="handleChange('status',$event)">
                    <a-select-option v-for="(item,index) in statusList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="是否漏油/气/水" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isOilLeak"
                  style="margin-bottom: 10px;">
                  <a-checkbox-group :disabled="disabled" placeholder="请选择是否漏油/气/水" 
                    name="checkboxgroup" :options="isOilLeakList" @change="onChange('isOilLeak',$event)" />
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row>
              <a-col :span="12">
                <a-form-model-item label="安全装置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="safetyDevice"
                  style="margin-bottom: 10px;">
                  <textarea :disabled="disabled" v-model="model.safetyDevice" cols="100" rows="3"
                    style="width: 100%;padding:0 30px 0 10px;line-height: 25px;" maxlength="100"
                    placeholder="请输入安全装置"></textarea>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="附属装置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accessoryDevice"
                  style="margin-bottom: 10px;">
                  <textarea :disabled="disabled" v-model="model.accessoryDevice" cols="100" rows="3"
                    style="width: 100%;padding:0 30px 0 10px;line-height: 25px;" maxlength="100"
                    placeholder="请输入附属装置"></textarea>
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row>
              <a-col :span="12">
                <a-form-model-item label="检查人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rummagerUserName"
                  style="margin-bottom: 10px;">
                  <div style="display: flex;align-items: center;">
                    <a-select :disabled="disabled" placeholder="请选择项目" :value="model.rummagerProjectName"
                      @change="handleChange('rummagerProject',$event)" style="margin-right: 10px;width: 112px;">
                      <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.rummagerProjectId ===''" placeholder="请选择部门" :value="model.rummagerDepartName"
                      @change="handleChange('rummagerDepart',$event)" style="margin-right: 10px;width: 112px;">
                      <a-select-option v-for="(item,index) in rummagerDepartList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled  || model.rummagerDepartId ===''" placeholder="请选择人员" :value="model.rummagerUserName"
                      @change="handleChange('rummagerUser',$event)" style="width: 112px;">
                      <a-select-option v-for="(item,index) in rummagerUserList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                  </div>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="保管人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="preserverUserName"
                  style="margin-bottom: 10px;">
                  <div style="display: flex;align-items: center;">
                    <a-select :disabled="disabled" placeholder="请选择项目" :value="model.preserverProjectName"
                      @change="handleChange('preserverProject',$event)" style="margin-right: 10px;width: 112px;">
                      <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.preserverProjectId ==='' " placeholder="请选择部门" :value="model.preserverDepartName"
                      @change="handleChange('preserverDepart',$event)" style="margin-right: 10px;width: 112px;">
                      <a-select-option v-for="(item,index) in preserverDepartList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.preserverDepartId ===''" placeholder="请选择人员" :value="model.preserverUserName"
                      @change="handleChange('preserverUser',$event)" style="width: 112px;">
                      <a-select-option v-for="(item,index) in preserverUserList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                  </div>
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row>
              <a-col :span="24">
                <a-form-model-item label="进场存放位置" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="address"
                  style="margin-bottom: 10px;">
                  <div style="display: flex;align-items: center;">
                    <a-select :disabled="disabled" placeholder="请选择项目" :value="model.addressProjectName"
                      @change="handleChange('addressProject',$event)" style="margin-right: 10px;width: 283px;">
                      <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.addressProjectId ===''" placeholder="请选择设备位置"
                      :value="model.addressDepartName" @change="handleChange('addressDepart',$event)"
                      style="margin-right: 10px;width: 283px;">
                      <a-select-option v-for="(item,index) in addressDepartList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.addressDepartId ===''" placeholder="请选择设备位置"
                      :value="model.address" @change="handleChange('address',$event)" style="width: 283px;">
                      <a-select-option v-for="(item,index) in addressList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                  </div>
                </a-form-model-item>
              </a-col>
            </a-row>
          </template>
          
          <template v-else-if="title === '新增退场单'">
            <a-row>
              <a-col :span="12">
                <a-form-model-item label="检测设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="statusName"
                  style="margin-bottom: 10px;">
                  <a-select :disabled="disabled" placeholder="请选择检测设备状态" :value="model.statusName"
                    @change="handleChange('status',$event)">
                    <a-select-option v-for="(item,index) in statusList" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="检查人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rummager"
                  style="margin-bottom: 10px;">
                  <div style="display: flex;align-items: center;">
                    <a-select :disabled="disabled" placeholder="请选择项目" :value="model.rummagerProjectName"
                      @change="handleChange('rummagerProject',$event)" style="margin-right: 10px;width: 112px;">
                      <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.rummagerProjecttId ===''" placeholder="请选择部门" :value="model.rummagerDepartName"
                      @change="handleChange('rummagerDepart',$event)" style="margin-right: 10px;width: 112px;">
                      <a-select-option v-for="(item,index) in rummagerDepartList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled  || model.rummagerDepartId ===''" placeholder="请选择人员" :value="model.rummagerUserName"
                      @change="handleChange('rummagerUser',$event)" style="width: 112px;">
                      <a-select-option v-for="(item,index) in rummagerUserList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                  </div>
                </a-form-model-item>
              </a-col>
            </a-row>
            
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="退场地点" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="address"
                  style="margin-bottom: 10px;">
                  <div style="display: flex;align-items: center;">
                    <a-select :disabled="disabled" placeholder="请选择项目" :value="model.addressProjectName"
                      @change="handleChange('addressProject',$event)" style="margin-right: 10px;width: 283px;">
                      <a-select-option v-for="(item,index) in projectList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.addressProjectId ===''" placeholder="请选择设备位置"
                      :value="model.addressDepartName" @change="handleChange('addressDepart',$event)"
                      style="margin-right: 10px;width: 283px;">
                      <a-select-option v-for="(item,index) in addressDepartList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                    <a-select :disabled="disabled || model.addressDepartId ===''" placeholder="请选择设备位置"
                      :value="model.address" @change="handleChange('address',$event)" style="width: 283px;">
                      <a-select-option v-for="(item,index) in addressList" :key="index" :value="item">
                        {{item.label}}
                      </a-select-option>
                    </a-select>
                  </div>
                </a-form-model-item>
              </a-col>
            </a-row>

            <a-row>
              <a-col :span="12">
                <a-form-model-item label="退场原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="retreatCause"
                  style="margin-bottom: 10px;">
                  <a-input :disabled="disabled" v-model="model.retreatCause" placeholder="请输入退场原因"></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
          </template>

          <a-row>
            <a-col :span="24">
              <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
                style="margin-bottom: 10px;">
                <div style="position: relative;">
                  <textarea :disabled="disabled" v-model="model.remark" cols="100" rows="3"
                    style="width: 95%;padding:0 30px 0 10px;line-height: 25px;" maxlength="500"
                    placeholder="请输入备注"></textarea>
                  <div style="position: absolute;right: 64px;bottom: 20px;font-size: 12px;">{{model.remark.length}}/500
                  </div>
                </div>
              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col :span="24">
              <a-form-model-item label="上传图片" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="informationUrl"
                style="margin-bottom: 10px;">
                <j-upload v-model="model.informationUrl" fileType="image" :disabled="disabled"
                  :buttonVisible="!disabled" text="上传设备图片"></j-upload>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
    <select-device-modal ref="modalForm" @ok="modalFormOk"></select-device-modal>
  </j-modal>
</template>

<script>
  import selectDeviceModal from './selectDeviceModal'
  import {
    httpAction,
    getAction,
    postAction
  } from '@/api/manage'
  export default {
    name: 'WtDevicetypeModal',
    components: {
      selectDeviceModal,
    },
    props: {
      pageType: {
        type: String,
        default: '', //add-新增 anew-重复提交 edit-编辑 detail-详情 approver-审批
      },
    },
    data() {
      return {
        title: '',
        width: 1100,
        visible: false,
        disableSubmit: false,
        //表单
        confirmLoading: false,
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 6
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          }
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 3
          }
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 21
          }
        },
        labelCol3: {
          xs: {
            span: 24
          },
          sm: {
            span: 12
          }
        },
        wrapperCol3: {
          xs: {
            span: 24
          },
          sm: {
            span: 12
          }
        },
        validatorRules: {},
        model: {
          equipmentId: '',
          type: '', //1 进场单 2 退场单
          filingTime: '',
          filingProjectName: '',
          filingProjectId: '',
          filingDepartName:'',
          filingDepartId:'',
          filingUserName:'',
          filingUserId:'',
          recordNumber: '',
          recordTime: '',
          statusName: '',
          status: '',
          isOilLeak: '0',
          safetyDevice: '',
          accessoryDevice: '',
          rummagerProjectName:'',
          rummagerProjectId:'',
          rummagerDepartName:'',
          rummagerDepartId:'',
          rummagerUserName:'',
          rummagerUserId:'',
          preserverProjectName:'',
          preserverProjectId:'',
          preserverDepartName:'',
          preserverDepartId:'',
          preserverUserName:'',
          preserverUserId:'',
          addressProjectName:'',
          addressProjectId:'',
          addressDepartName:'',
          addressDepartId:'',
          address: '',
          remark: '',
          informationUrl: '',
          //退场单
          retreatCause: '',
        },
        projectList: [], //项目列表
        filingDepartList:[], //填写人-部门
        filingUserList:[], //填写人-人员
        rummagerDepartList:[], //检查人-部门
        rummagerUserList:[], //检查人-人员
        preserverDepartList:[], //保管人-部门
        preserverUserList:[], //保管人-人员
        addressDepartList:[], //位置-部门
        addressList:[], //位置-地址
        statusList: [{
          label: '较差',
          value: '1'
        }, {
          label: '一般',
          value: '2'
        }, {
          label: ' 良好',
          value: '3'
        }], //设备状态
        retreatAddressList: [], //退场地点
        isOilLeakList: [{
          label: '无',
          value: '0'
        }, {
          label: '漏油',
          value: '1'
        }, {
          label: '漏气',
          value: '2'
        }, {
          label: '漏水',
          value: '3'
        }], //是否漏油/气/水
        columns: [{
            title: '设备编号',
            dataIndex: 'number',
            key: 'number',
            width: '20%',
          },
          {
            title: '归属项目',
            dataIndex: 'projectName',
            key: 'projectName',
            width: '25%',
          },
          {
            title: '设备类型',
            dataIndex: 'typeName',
            key: 'typeName',
            width: '15%',
          },
          {
            title: '设备名称',
            dataIndex: 'name',
            key: 'name',
            width: '20%',
          },
          {
            title: '规格型号',
            dataIndex: 'model',
            key: 'model',
            width: '20%',
          }, {
            title: '',
            dataIndex: 'deletes',
            key: 'deletes',
            width: '5%',
            scopedSlots: {
              customRender: 'deletes'
            }
          },
        ],
        deviceList: [], //设备列表
        selectedRowKeys: [],
        url: {
          add: '/equipment/wtEquipmentDetail/add',
          edit: '/equipment/wtEquipmentDetail/edit',
          list: '',
          detail: '/equipment/wtEquipmentDetail/queryById',
          delete: '',
        },
        disabled: false,
      }
    },
    created() {

    },
    mounted() {

    },
    methods: {
      remove(key) {
        const newData = this.deviceList.filter(item => item.key !== key);
        this.deviceList = newData;
      },
      //表格数据批量删除
      handleDelCom() {
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var _self = this;
          this.$confirm({
            title: "确认删除",
            content: "是否删除选中数据?",
            onOk: function() {
              let deviceList = JSON.parse(JSON.stringify(_self.deviceList));
              deviceList.forEach((item, index) => {
                _self.$set(item, 'is_sel', false)
                _self.selectedRowKeys.forEach((v, i) => {
                  if (item.key === v) {
                    item.is_sel = true
                  }
                })
              })
              let deviceLists = []
              deviceList.forEach(ite => {
                if (!ite.is_sel) {
                  deviceLists.push(ite)
                }
              })
              _self.selectedRowKeys = [];
              _self.deviceList = deviceLists;
            }
          });
        }
      },
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys;
      },
      modalFormOk(deviceList) {
        this.deviceList = deviceList;
        this.$forceUpdate();
      },
      handleSelect(type, record) {
        this.$refs.modalForm.title = record;
        this.$refs.modalForm.disabled = false;
        this.$refs.modalForm.edit(type, record);
      },
      gettime() {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconds = date.getSeconds();
        //this.model.filingTime = year + "-" + month + "-" + day + " "  + hours + ":" + minutes+":" + seconds;
        this.model.filingTime = year + "-" + this.getZero(month) + "-" + this.getZero(day) + " " + this.getZero(hours) + ":" + this.getZero(minutes)+ ":" + this.getZero(seconds);
      },
      //个位数补0
      getZero(num) {
        // 单数前面加0
        if (parseInt(num) < 10) {
      	num = '0' + num;
        }
        return num;
      },
      onChange(type, item, ) {
        console.log(1, type, item);
        if (type === 'isOilLeak') {
          let isOilLeak = ''
          if(item && item.length>0){
            item.forEach(ite=>{
              if(!!isOilLeak){
                isOilLeak += ','+ite
              }else{
                isOilLeak += ite
              }
            })
          }
          this.model.isOilLeak = isOilLeak;
        }
      },
      //下拉选择
      handleChange(type, item, index) {
        console.log(1, type, item, index);
        let _self = this;
        if(type === 'filingProject'){ //填写人-项目
          this.model.filingProjectName = item.label;
          this.model.filingProjectId = item.value;
           //清空联动数据
          this.model.filingDepartName = '';
          this.model.filingDepartId = '';
          /* getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.filingProjectId}`).then((res) => {
              if (res.success) {
                this.filingDepartList = [];
                let treeList = res.result;
               this.filingDepartList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.departName,
               }))
               this.$forceUpdate();
              }
            }) */
        } else if(type === 'filingDepart'){ //填写人-部门
          this.model.filingDepartName = item.label;
          this.model.filingDepartId = item.value;
          //清空联动数据

        } else if(type === 'filingUser'){ //填写人-人员
          this.model.filingUserName = item.label;
          this.model.filingUserId = item.value;
          
        } else if(type === 'rummagerProject'){ //检查人-项目
          this.model.rummagerProjectName = item.label;
          this.model.rummagerProjectId = item.value;
           //清空联动数据
          this.model.rummagerDepartName = '';
          this.model.rummagerDepartId = '';
          this.model.rummagerUserName = '';
          this.model.rummagerUserId = '';
          getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.rummagerProjectId}`).then((res) => {
              if (res.success) {
                this.rummagerDepartList = [];
                let treeList = res.result;
               this.rummagerDepartList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.departName,
               }))
               this.$forceUpdate();
              }
            })
        } else if(type === 'rummagerDepart'){ //检查人-部门
          this.model.rummagerDepartName = item.label;
          this.model.rummagerDepartId = item.value;
           //清空联动数据
          this.model.rummagerUserName = '';
          this.model.rummagerUserId = '';
          getAction(`/sys/sysDepart/getUsersByDepartId?id=${this.model.rummagerDepartId}`).then((res) => {
              if (res.success) {
                this.rummagerUserList = [];
                let treeList = res.result;
               this.rummagerUserList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.realname,
               }))
               this.$forceUpdate();
              }
            })
        } else if(type === 'rummagerUser'){ //检查人-人员
          this.model.rummagerUserName = item.label;
          this.model.rummagerUserId = item.value;
        } else if(type === 'preserverProject'){ //保管人-项目
          this.model.preserverProjectName = item.label;
          this.model.preserverProjectId = item.value;
          //清空联动数据
          this.model.preserverDepartName = '';
          this.model.preserverDepartId = '';
          this.model.preserverUserName = '';
          this.model.preserverUserId = '';
          getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.preserverProjectId}`).then((res) => {
              if (res.success) {
                this.preserverDepartList = [];
                let treeList = res.result;
               this.preserverDepartList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.departName,
               }))
               this.$forceUpdate();
              }
            })
        } else if(type === 'preserverDepart'){ //保管人-部门
          this.model.preserverDepartName = item.label;
          this.model.preserverDepartId = item.value;
          //清空联动数据
          this.model.preserverUserName = '';
          this.model.preserverUserId = '';
          getAction(`/sys/sysDepart/getUsersByDepartId?id=${this.model.preserverDepartId}`).then((res) => {
              if (res.success) {
                this.preserverUserList = [];
                let treeList = res.result;
               this.preserverUserList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.realname,
               }))
               this.$forceUpdate();
              }
            })
        } else if(type === 'preserverUser'){ //保管人-人员
          this.model.preserverUserName = item.label;
          this.model.preserverUserId = item.value;
          
        } else if(type === 'addressProject'){ //位置-项目
          this.model.addressProjectName = item.label;
          this.model.addressProjectId = item.value;
          //清空联动数据
          this.model.addressDepartName = '';
          this.model.addressDepartId = '';
          this.model.address = '';
          getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.addressProjectId}`).then((res) => {
              if (res.success) {
                this.addressDepartList = [];
                let treeList = res.result;
               this.addressDepartList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.departName,
               }))
               this.$forceUpdate();
              }
            })
        } else if(type === 'addressDepart'){ //位置-部门
          this.model.addressDepartName = item.label;
          this.model.addressDepartId = item.value;
          //清空联动数据
          this.model.address = '';
          getAction(`/equipment/wtEquipmentAddress/getWtEquipmentAddressList?departId=${this.model.addressDepartId}&projectId=&address=`).then((res) => {
              if (res.success) {
                this.addressList = [];
                let treeList = res.result;
               this.addressList = [...treeList].map((v) => ({
                 ...v,
                 value: v.id,
                 label: v.address,
               }))
               this.$forceUpdate();
              }
            })
        } else if(type === 'address'){ //位置-地址
          this.model.address = item.label;
          
        } else if (type === 'teamGroup') {
          this.model.teamGroupName = item.label;
          this.model.teamGroup = item.value;
        } else if (type === 'supplier') {
          this.model.supplierName = item.label;
          this.model.supplier = item.value;
        } else if (type === 'sourceType') {
          this.model.sourceTypeName = item.label;
          this.model.sourceType = item.value;
        } else if (type === 'status') {
          this.model.statusName = item.label;
          this.model.status = item.value;
        } else if (type === 'state') {
          this.model.stateName = item.label;
          this.model.state = item.value;
        } else if (type === 'termService') {
          this.model.termServiceName = item.label;
          this.model.termService = item.value;
        }
        this.$forceUpdate();
      },
      //获取数据字典列表
      Dictionary() {
        var _self = this;
        //项目列表
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {
            this.projectList = [];
            let treeList = res.result;
            this.projectList = [...treeList].map((v) => ({
              ...v,
              value: v.id,
              label: v.departName,
            }))
          }
        })


        //设备位置列表
        getAction(`/equipment/wtEquipmentAddress/list`).then((res2) => {
          if (res2.success) {
            let treeList = res2.result.records
            if (treeList && treeList.length > 0) {
              this.addressList = [...treeList].map((v) => ({
                ...v,
                value: v.id,
                label: v.address,
              }))
            }
          }
        })
        
         //用户部门
        getAction(`/sys/user/getCurrentUserDeparts`).then((res) => {
            if (res.success) {
              this.filingDepartList = [];
              let treeList = res.result.list;
             this.filingDepartList = [...treeList].map((v) => ({
               ...v,
               value: v.id,
               label: v.departName,
             }))
            }
          })

        //班组列表
        getAction(`/wtSupplier/list?type=1`).then((res2) => {
          if (res2.success) {
            let treeList = res2.result.records
            if (treeList && treeList.length > 0) {
              this.teamGroupList = [...treeList].map((v) => ({
                ...v,
                value: v.id,
                label: v.name,
              }))
            }
            if (this.pageType === 'edit' || this.pageType === 'detail') {
              let info = this.teamGroupList.find((item) => item.value == this.model.teamGroup);
              if (!!info) {
                this.model.teamGroupName = info.label;
              }
            }
          }
        })

        //供应商列表
        getAction(`/wtSupplier/list?type=0`).then((res) => {
          if (res.success) {
            let treeList = res.result.records;
            if (treeList && treeList.length > 0) {
              this.supplierList = [...treeList].map((v) => ({
                ...v,
                value: v.id,
                label: v.name,
              }))
            }
            if (this.pageType === 'edit' || this.pageType === 'detail') {
              let info = this.supplierList.find((item) => item.value == this.model.supplier);
              if (!!info) {
                this.model.supplierName = info.label;
              }
            }
          }
        })
        if (this.pageType === 'edit' || this.pageType === 'detail') {
          if (this.sourceTypeList && this.sourceTypeList.length > 0) {
            let info = this.sourceTypeList.find((item) => item.value == this.model.sourceType);
            if (!!info) {
              this.model.sourceTypeName = info.label;
            }
          }
          if (this.statusList && this.statusList.length > 0) {
            let info = this.statusList.find((item) => item.value == this.model.status);
            if (!!info) {
              this.model.statusName = info.label;
            }
          }
          if (this.stateList && this.stateList.length > 0) {
            let info = this.stateList.find((item) => item.value == this.model.state);
            if (!!info) {
              this.model.stateName = info.label;
            }
          }
          if (this.termServiceList && this.termServiceList.length > 0) {
            let info = this.termServiceList.find((item) => item.value == this.model.termService);
            if (!!info) {
              this.model.termServiceName = info.label;
            }
          }
          if (!!this.model.projectId) {
            getAction(`/sys/sysDepart/getDepartListByParentId?parentId=${this.model.projectId}`).then((res) => {
              if (res.success) {
                this.departList = [];
                let treeList = res.result;
                this.departList = [...treeList].map((v) => ({
                  ...v,
                  value: v.id,
                  label: v.departName,
                }))
                let info = this.departList.find((item) => item.value == this.model.departId);
                if (!!info) {
                  this.model.departName = info.label;
                }
              }
            })
          }
          if (!!this.model.departId) {
            getAction(`/sys/sysDepart/getUsersByDepartId?id=${this.model.departId}`).then((res) => {
              if (res.success) {
                this.departList = [];
                let treeList = res.result;
                this.userList = [...treeList].map((v) => ({
                  ...v,
                  value: v.id,
                  label: v.realname,
                }))
                let info = this.userList.find((item) => item.value == this.model.userId);
                if (!!info) {
                  this.model.userName = info.label;
                }
              }
            })
          }

        }
      },
      add(type, record) {
        console.log(11,type,record)
        this.visible = true;
        this.pageType = type;
        this.init();
      },
      edit(type, record) {
        this.visible = true;
        this.pageType = type;
        this.init(record.id);
      },
      init(id) {
        if(this.pageType === 'add'){
          this.model = {
            equipmentId: '',
            type: '', //1 进场单 2 退场单
            filingTime: '',
            filingProjectName: '',
            filingProjectId: '',
            filingDepartName:'',
            filingDepartId:'',
            filingUserName:'',
            filingUserId:'',
            recordNumber: '',
            recordTime: '',
            statusName: '',
            status: '',
            isOilLeak: '0',
            safetyDevice: '',
            accessoryDevice: '',
            rummagerProjectName:'',
            rummagerProjectId:'',
            rummagerDepartName:'',
            rummagerDepartId:'',
            rummagerUserName:'',
            rummagerUserId:'',
            preserverProjectName:'',
            preserverProjectId:'',
            preserverDepartName:'',
            preserverDepartId:'',
            preserverUserName:'',
            preserverUserId:'',
            addressProjectName:'',
            addressProjectId:'',
            addressDepartName:'',
            addressDepartId:'',
            address: '',
            remark: '',
            informationUrl: '',
            //退场单
            retreatCause: '',
          };
        }
        if (this.pageType === 'add' || this.pageType === 'edit') {
          if(this.title === '新增进场单'){
            this.model.type = '1';
          }else if(this.title === '新增退场单'){
            this.model.type = '2';
          }
          this.deviceList = [];
          if (!!window.localStorage.getItem("pro__Login_Userinfo")) {
            let userInfo = JSON.parse(window.localStorage.getItem("pro__Login_Userinfo")).value;
            this.model.filingUserId = userInfo.id;
            this.model.filingUserName = userInfo.realname;
            let userInfos = [];
            userInfos.push(userInfo);
            this.filingUserList = userInfos.map(v => ({
              ...v,
              value: v.id,
              label: v.realname,
            }));
          }
            
          
          this.gettime();
          if (this.title === '新增进场单') {
            this.validatorRules = {
              equipmentId: [{
                required: true,
                message: '设备不能为空,请选择~',
                trigger: 'change'
              }],
              recordNumber: [{
                required: true,
                message: '进场单号不能为空,请输入~',
                trigger: 'blur'
              }],
              recordTime: [{
                required: true,
                message: '进场时间不能为空,请输入~',
                trigger: 'blur'
              }],
              statusName: [{
                required: true,
                message: '检测设备状态不能为空,请选择~',
                trigger: 'change'
              }],
              isOilLeak: [{
                required: true,
                message: '是否漏油/气/水不能为空,请选择~',
                trigger: 'change'
              }],
              safetyDevice: [{
                required: true,
                message: '安全装置不能为空,请输入~',
                trigger: 'blur'
              }],
              accessoryDevice: [{
                required: true,
                message: '附属装置不能为空,请输入~',
                trigger: 'blur'
              }],
              rummagerUserName: [{
                required: true,
                message: '检查人不能为空,请选择~',
                trigger: 'change'
              }],
              preserverUserName: [{
                required: true,
                message: '保管人不能为空,请选择~',
                trigger: 'change'
              }],
              address: [{
                required: true,
                message: '进场存放位置不能为空,请选择~',
                trigger: 'change'
              }],
              informationUrl: [{
                required: true,
                message: '上传资料不能为空,请选择~',
                trigger: 'blur'
              }],
            }
          } else if (this.title === '新增退场单') {
            this.validatorRules = {
              equipmentId: [{
                required: true,
                message: '设备不能为空,请选择~',
                trigger: 'change'
              }],
              recordNumber: [{
                required: true,
                message: '退场单号不能为空,请输入~',
                trigger: 'blur'
              }],
              recordTime: [{
                required: true,
                message: '退场时间不能为空,请输入~',
                trigger: 'blur'
              }],
              statusName: [{
                required: true,
                message: '检测设备状态不能为空,请选择~',
                trigger: 'change'
              }],
              rummagerUserName: [{
                required: true,
                message: '检查人不能为空,请输入~',
                trigger: 'blur'
              }],
              retreatCause: [{
                required: true,
                message: '退场原因不能为空,请输入~',
                trigger: 'blur'
              }],
              address: [{
                required: true,
                message: '退场地点不能为空,请选择~',
                trigger: 'change'
              }],
              informationUrl: [{
                required: true,
                message: '上传资料不能为空,请选择~',
                trigger: 'blur'
              }],
            }
          }

        }
        if (this.pageType === 'edit' || this.pageType === 'detail') {
          //供应商列表
          getAction(`/equipment/wtEquipment/queryById?id=${id}`).then((res) => {
            if (res.success) {
              this.model = res.result;
              this.Dictionary();
            }
          })
        } else if (this.pageType === 'add') {
          this.Dictionary();
        }

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const _self = this;
        let equipmentId = '';
        if(this.deviceList && this.deviceList.length>0){
          this.deviceList.forEach(ite=>{
            if(!!equipmentId){
              equipmentId += ',' + ite.id;
            }else{
              equipmentId += ite.id;
            }
          })
        }
        this.model.equipmentId = equipmentId;
        if (this.pageType === 'add' || this.pageType === 'edit') {
          if (!this.model.equipmentId) {
            _self.$message.warning('设备不能为空,请选择');
            return;
          }
          if (!this.model.recordNumber) {
            _self.$message.warning(this.title === '新增进场单'?'进场单号不能为空,请输入~':'退场单号不能为空,请输入~');
            return;
          }
          if (!this.model.recordTime) {
            _self.$message.warning(this.title === '新增进场单'?'进场时间不能为空,请选择~':'退场时间不能为空,请选择~');
            return;
          }
          if (!this.model.statusName) {
            _self.$message.warning('检测设备状态不能为空,请选择');
            return;
          }
          if(this.title === '新增进场单'){
            if (!this.model.isOilLeak) {
              _self.$message.warning('是否漏油/气/水不能为空,请选择');
              return;
            }
            if (!this.model.safetyDevice) {
              _self.$message.warning('安全装置不能为空,请输入');
              return;
            }
            if (!this.model.accessoryDevice) {
              _self.$message.warning('附属装置不能为空,请输入');
              return;
            }
            if ((!this.model.preserverProjectName) || (!this.model.preserverDepartName) || (!this.model.preserverUserName)) {
              _self.$message.warning('保管人不能为空,请选择');
              return;
            }
          }else if(this.title === '新增进退场单'){
            if (!this.model.retreatCause) {
              _self.$message.warning('退场原因不能为空,请输入');
              return;
            }
          }
          if ((!this.model.rummagerProjectName) || (!this.model.rummagerDepartName) || (!this.model.rummagerUserName)) {
            _self.$message.warning('检查人不能为空,请选择');
            return;
          }
          if ((!this.model.addressProjectName) || (!this.model.addressDepartName) || (!this.model.address)) {
            _self.$message.warning(this.title === '新增进场单'?'进场存放位置不能为空,请选择':'退场地点不能为空,请输入~');
            return;
          }
          if (!this.model.informationUrl) {
            _self.$message.warning('上传图片不能为空,请选择');
            return;
          }
        }
        let params = {};
        let httpurl = ''
        if (_self.pageType === 'add') {
          httpurl = _self.url.add;
          params = _self.model;
        } else if (_self.pageType === 'edit') {
          httpurl = _self.url.edit;
          params = _self.model;
        }
        // 触发表单验证
        _self.confirmLoading = true;
        console.log('提交表单', params)
        // debugger
        httpAction(httpurl, params, 'POST')
          .then((res) => {
            if (res.success) {
              _self.$message.success(res.message)
              _self.submitCallback();
            } else {
              _self.$message.warning(res.message)
            }
          })
          .finally(() => {
            _self.confirmLoading = false
          })
      },
      submitCallback() {
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel() {
        this.close()
      }
    }
  }
</script>
<style scoped>
  .scroll-view {
    overflow: auto;
    overflow-y: scroll;
  }
</style>